<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib  prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"></c:set>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="${ctx }/js/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
	//当前的页数
	var currentPage = 1;
	//每页的数量
	var numberPage = 5;
	//总数据
	var total = 0;
	//总页数
	var endPage = 0;
	//查询图书
	queryBook();
	//显示金额
	showMoney();
	//查询购物车
	queryCart();
	function queryBook(){
		$.ajax({
			url:"${ctx}/showBook",
			type:"post",
			data:{
				"currentPage":currentPage,
				"numberPage":numberPage
			},
			success:function(data){
				console.log(data);
				total = data.total;
				//alert(total);
				var content = "";
				content+="<table border='1' cellpadding='10' cellspacing='0'>";
				content+="<caption>图书列表</caption>";
				content+="<tr>";
				content+="<th>图书名</th>";
				content+="<th>库存</th>";
				content+="<th>单价</th>";
				content+="<th>购买</th>";
				content+="<th>加入购物车</th>";
				content+="</tr>";
				for(var i = 0;i<data.books.length;i++){
					content+="<tr>";
					content+="<td>"+data.books[i].bookName+"</td>";
					content+="<td>"+data.books[i].bookStock+"</td>";
					content+="<td>"+data.books[i].bookPrice+"</td>";
					content+="<td><a class='buyBook' href='"+data.books[i].bookId+"'>购买</a></td>";
					content+="<td><a class='addCart' href='"+data.books[i].bookId+"'>加入购物车</a></td>";
					content+="</tr>";
				}
				content+="</table>";
				controllerButton();
				$("#showBook").html(content);
				$("#message").html("<h3>当前页："+currentPage+"页，总页数:"+endPage+"页，总数据："+total+"笔</h3>");
			}
		});
	}
	//分页的按钮控制
	function controllerButton(){
		if(total%numberPage==0){
			endPage = total/numberPage;
		}else{
			endPage = parseInt(total/numberPage)+1;
		}
		if(currentPage==1){
			$(".firstPage").eq(0).prop("disabled",true);
			$(".prePage").eq(0).prop("disabled",true);
			$(".nextPage").eq(0).prop("disabled",false);
			$(".lastPage").eq(0).prop("disabled",false);
		}
		if(currentPage==endPage){
			$(".firstPage").eq(0).prop("disabled",false);
			$(".prePage").eq(0).prop("disabled",false);
			$(".nextPage").eq(0).prop("disabled",true);
			$(".lastPage").eq(0).prop("disabled",true);
		}
	}
	//第一页
	$(".firstPage").eq(0).click(function(){
		currentPage = 1;
		queryBook();
	});
	//上一页
	$(".prePage").eq(0).click(function(){
		currentPage = currentPage-1;
		queryBook();
	});
	//下一页
	$(".nextPage").eq(0).click(function(){
		currentPage = currentPage+1;
		queryBook();
	});
	//最后一页
	$(".lastPage").eq(0).click(function(){
		currentPage = endPage;
		queryBook();
	})
	function queryCart(){
		$.ajax({
			url:"${ctx}/showCart",
			type:"post",
			data:{
				"userId":'${sessionScope.user.userId}'
			},
			success:function(data){
				//alert(1);
				//console.log(data);
				var length = data.length;
				var contentCart = "";
				contentCart+="<table border='1' cellpadding='10' cellspacing='0'>";
				contentCart+="<caption>购物车列表</caption>";
				contentCart+="<tr>";
				contentCart+="<th>图书名</th>";
				contentCart+="<th>库存</th>";
				contentCart+="<th>移除</th>";
				contentCart+="</tr>";
				for(var i = 0;i<data.length;i++){
					contentCart+="<tr>";
					contentCart+="<td>"+data[i].book.bookName+"</td>";
					contentCart+="<td>"+data[i].cartNumber+"</td>";
					contentCart+="<td><a class='removeBook' href='"+data[i].bookId+"'>移除</a></td>";
					contentCart+="<td><a class='submitCart' href='"+data[i].bookId+"' id='"+data[i].cartNumber+"'>提交购物车</a></td>";
					contentCart+="</tr>";
				}
				
				contentCart+="</table>";
				
				$("#showCart").html(contentCart);
			}
		});
	}
	//提交购物车
	$("#showCart").delegate(".submitCart","click",function(){
		var bookId = $(this).attr("href");
		var cartNumber =$(this).attr("id");
		var userId = '${sessionScope.user.userId}';
		var url = "${ctx}/submitCart";
		var submitdata={
				"bookId":bookId,
				"userId":userId,
				"cartNumber":cartNumber
		}
		$.post(url,submitdata,function(data){
			if(data!="error"){				
				alert("提交成功！")
			}else{
				alert("图书的库存或者用户的余额不足！")
			}
		})
		queryBook();
		showMoney();
		queryCart();
		return false;
	});
	//移除购物车
	$("#showCart").delegate(".removeBook","click",function(){
		var bookId = $(this).attr("href");
		var userId = '${sessionScope.user.userId}';
		var url = "${ctx}/deleteCart";
		var data ={
				"bookId":bookId,
				"userId":userId
		}
		$.post(url,data,function(data){
			alert("移除成功！")
		})
		queryCart();
		return false;
	});
	function showMoney(){
		var userId = '${sessionScope.user.userId}';
		$.ajax({
			url:"${ctx}/selectMoney",
			type:"post",
			data:{
				"userId":userId
			},
			success:function(data){
				//console.log(data);
				$("#showmoney").attr("value",data);
			}
		});
	}
	//买书
	$("#showBook").delegate(".buyBook","click",function(){
		var bookId = $(this).attr("href");
		var userId = '${sessionScope.user.userId}';
		var url = "${ctx}/buyBook";
		var data ={
				"bookId":bookId,
				"userId":userId
		}
		$.post(url,data,function(data){
			if(data!="error"){
				alert("购买成功！")
			}else{
				alert("图书的库存或用户余额不足！");
			}			
		})
		showMoney();
		queryBook();
		return false;
	});
	//添加购物车
	$("#showBook").delegate(".addCart","click",function(){
		var bookId = $(this).attr("href");
		var url = "${ctx}/addCart";
		var userId = '${sessionScope.user.userId}';
		var data ={
				"bookId":bookId,
				"userId":userId
		}
		$.post(url,data,function(data){
			alert("添加购物车成功！")
		})
		queryCart();
		return false;
	});
	//充值
	$("#addmoney").click(function(){
		$("#addForm").stop().slideToggle(1000);
		return false;
	});
	$("#addForm").submit(function(){
		var accId = '${sessionScope.user.userId}';
		var money = $("#money").val();
		$.ajax({
			url:"${ctx}/addMoney",
			data:{
				"money":money,
				"userId":accId
			},
			type:"post",
			success:function(data){
				if(data){
					alert("充值成功！");
				}else{
					alert("充值失败！");
				}
			}
		});
		showMoney();
		return false;
	});
	//提交所有的购物车
	$("#submitAllCart").click(function(){
		$.post("${ctx}/submitAllCart",{"userId":'${sessionScope.user.userId}'},function(data){
			if(data!="error"){
				alert("一键提交成功！")
			}else{
				alert("用户的余额或者图书的库存不足！")
			}
		});
		queryBook();
		showMoney();
		queryCart();
	});
	//单击按钮实现跳转
	
});
</script>
<title>Insert title here</title>
</head>
<body>
<h3>欢迎你，${sessionScope.user.userName },你的余额：<input id="showmoney" disabled="disabled" style="width:50px;"/>，<a id="addmoney" href="" >充值</a></h3>
<hr>
<form id="addForm" style="display:none;">
	充值金额：<input type="text" id="money">
	<input type="submit" value="充值" >
</form>
<div id="showBook"></div>
<button class="firstPage">first</button>
<button class="prePage">pre</button>
<button class="nextPage">next</button>
<button class="lastPage">last</button>
<span id="message"></span>
<hr>
<div id="showCart" >
</div>
<input type="button" id="submitAllCart" value="一键提交"/>
<hr>
</body>
</html>